package juc.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author Guo
 * @Create 2020/9/2 10:15
 */
public class CompltableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 无返回结果
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "没有返回 update mysql ok ");
        });
        completableFuture.get();

        //异步回调 有返回结果
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "有返回");
            int i = 10/0;
            return 1024;
        });

        System.out.println(completableFuture2.whenComplete((t, u) -> {
            //t 结果 u异常
            System.out.println("******t:" + t);
            System.out.println("******u:" + u);
        }).exceptionally(f -> {
            System.out.println("****exception:" + f.getMessage());
            return 4444;
        }).get());

    }
}
